package com.vs.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vs.common.SessionKeys;
import com.vs.dao.ProjectOperations;
import com.vs.model.CustomMessage;
import com.vs.model.ProjectForm;
import com.vs.model.User;
import com.vs.util.Constants;

public class ProjectAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String next = "";
		ProjectForm projectForm = (ProjectForm) form;
		Constants.log.debug(projectForm);

		List<ProjectForm> prjList = null;
		ArrayList<CustomMessage> customErrors = new ArrayList<CustomMessage>();

		String action = request.getParameter("action");
		System.out.println("action1=" + action);

		if (action == null) {
			action = projectForm.getAction();
			System.out.println("action2=" + action);
		}

		if (action != null) {
			if (action.equals("selectAll")) {
				next = "selectAll";
			}
			else if (action.equals("select")) {
				Integer projectId = projectForm.getProjectId();
				request.setAttribute("projectId", projectId);
				next = "select";
			}
			else if (action.equals("add")) {

				User user = (User) getActiveSession(request).getAttribute(SessionKeys.CURRENT_USER);
				projectForm.setAddedBy(user.getUserName());
				boolean result = new ProjectOperations()
						.addProject(projectForm);

				if (result) {
					next = "selectAll";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("Project " + projectForm.getProjectName()
							+ " is added successfully");
					customErrors.add(info);

					updateProjectLOVInSession(request);

				} else {
					next = "addError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while saving project details");
					customErrors.add(info);
				}

			} else if (action.equals("beforeEdit")) {
				
				Integer projectId = projectForm.getProjectId();
				ProjectForm prjForm = new ProjectOperations()
						.getProject(projectId);
				if (prjForm!=null) {
					next = "beforeEdit";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("Found project details" + projectForm.getProjectId()
							);
					customErrors.add(info);
					request.setAttribute("prjObj", prjForm);

				} else {
					next = "editError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while fetching project details for Id " + projectId);
					customErrors.add(info);
				}
			} else if (action.equals("edit")) {
				boolean result = new ProjectOperations()
						.editProject(projectForm);
				if (result) {
					next = "selectAll";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("Project " + projectForm.getProjectName()
							+ " is edited successfully");
					customErrors.add(info);

					updateProjectLOVInSession(request);

				} else {
					next = "editError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while editing project details");
					customErrors.add(info);
				}
			} else if (action.equals("delete")) {

				// Integer projectId = Integer.parseInt(request
				// .getParameter("projectId"));
				Integer projectId = projectForm.getProjectId();

				boolean result = new ProjectOperations()
						.deleteProject(projectId);
				if (result) {
					next = "selectAll";
					
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("projectId " + projectId
							+ " is deleted successfully");
					customErrors.add(info);

					updateProjectLOVInSession(request);
				} else {
					next = "deleteError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Error while deleting project "
							+ projectId);
					customErrors.add(info);
				}
			}
		}
		
		if(next.equals("selectAll"))
		{
			prjList = new ProjectOperations().getAllProjectList();
		if(prjList==null || prjList.size()==0)
		{
			CustomMessage info = new CustomMessage();
			info.setMessageLevel(Constants.INFO);
			info.setMessageText("At present, no project are added into the system");
			customErrors.add(info);
		}
		}

		System.out.println("next="+next);
		request.setAttribute("customErrors", customErrors);
		request.setAttribute("projectList", prjList);
		System.out.println("added project list to request object");
		return mapping.findForward(next);

	}

	private HttpSession getActiveSession(HttpServletRequest request) {
		return request.getSession(false);
	}

	private void updateProjectLOVInSession(HttpServletRequest request) {
		HttpSession session = getActiveSession(request);
		session.removeAttribute("projectLOV");
		session.setAttribute("projectLOV",
				new ProjectOperations().getProjectLOV());
	}
}
